package net.oov.identify;

import net.oov.identify.util.CharExtUtil;

import org.apache.lucene.analysis.Token;

/**
 * @author huangsui.cn
 *
 */
public class SequenceSawInsertIns implements SequenceSawInsert{
	
	private SequenceInfo ss = null;//单字拼接
	
	//flag 标记分割字串左右邻接
	public static final int OOV_ADJACENCY_CJK = 1;//左/右邻接：汉字
	public static final int OOV_ADJACENCY_CJK_SINGLE = 2;//左/右邻接：单个汉字
	public static final int OOV_ADJACENCY_LETTER_DIGIT = 3;//左/右邻接：英文字母和数字
	public static final int OOV_ADJACENCY_NOSENSE = 4;//左/右邻接：special char eg.【】~，& .
	
	/**
	 * 
	 * 标示汉字串前后串类型
	 * @Override
	 * 
	 */
	public SequenceInfo markSpecialChar(Token tkPre, Token tk, Token tkNext) {

		//tk
		SequenceInfo si = null;
		if(tk==null){
			return null;
		}else if(tk.termLength()!=1){
			return null;
		}else if(!CharExtUtil.isCJKCharacter(tk.term())){//仅标示汉字且单字时返回有值
			return null;
		}else{
			si = new SequenceInfo();
			si.text = tk.term();
		}
		
		//tkPre tkNext 偏移量检查
		if(tkPre!=null&&tkPre.endOffset()!=tk.startOffset()){
			tkPre = null;//局部变量
		}
		if(tkNext!=null&&tkNext.startOffset()!=tk.endOffset()){
			tkNext = null;
		}
		//邻接类型标示
		if(tkPre == null){
			si.leftAdjacencyType = OOV_ADJACENCY_NOSENSE;
		}else if(tkPre.termLength()==1&&CharExtUtil.isCJKCharacter(tkPre.term())){//单字
			si.leftAdjacencyType = OOV_ADJACENCY_CJK_SINGLE;
		}else if(CharExtUtil.isNoSence(tkPre.term().charAt(tkPre.termLength()-1))){
			si.leftAdjacencyType = OOV_ADJACENCY_NOSENSE;
		}
		if(tkNext == null){
			si.rightAdjacenryType =  OOV_ADJACENCY_NOSENSE;
		}else if(tkNext.termLength()==1&&CharExtUtil.isCJKCharacter(tkNext.term())){
			si.rightAdjacenryType = OOV_ADJACENCY_CJK_SINGLE;
		}else if(CharExtUtil.isNoSence(tkNext.term().charAt(tkNext.termLength()-1))){
			si.rightAdjacenryType =  OOV_ADJACENCY_NOSENSE;
		}
		return si;
		
	}

	/**
	 * 
	 * 格式如下：
	 * -ABC-
	 * -ABC
	 * ABC-
	 * ABC
	 * @param str
	 * @Override
	 */
	public void addHandedCharSequence(SequenceFreqContainer sequenceFreqContainer,
			Token tkPre, Token tk, Token tkNext) {
		
		//debug
/*		if(tkPre!=null)	System.out.print(tkPre.term());
		if(tk!=null)	System.out.print(" "+tk.term());		
		if(tkNext!=null)	System.out.print(" "+tkNext.term());
		if(ss!=null)	System.out.print(" "+ss);
		System.out.println();*/
		
		
		SequenceInfo si = this.markSpecialChar(tkPre, tk, tkNext);
		if(si == null)return;

		//前邻接单个汉字
		if(si.leftAdjacencyType == OOV_ADJACENCY_CJK_SINGLE){
			ss.text = ss.text + si.text;
			ss.rightAdjacenryType = si.rightAdjacenryType;
		}else{//前面不邻接汉字
			ss = new SequenceInfo();
			ss.text = si.text;
			ss.leftAdjacencyType = si.leftAdjacencyType;
			ss.rightAdjacenryType = si.rightAdjacenryType;
		}
		//后邻接单个汉字
		if(si.rightAdjacenryType == OOV_ADJACENCY_CJK_SINGLE){
			return;
		}else{//后面不邻接汉字
			//往下执行
		}

		for(int offset=0;offset<ss.text.length();offset++){//truncating the sentence,start on the first char
			for(int segLen=1;segLen<=sequenceFreqContainer.getMaxSeqLengthAllowed()&&offset+segLen<=ss.text.length();segLen++){//truncated a char sequence from one char,its length less than or equals the threshold maxLen
				char[] cs = ss.text.substring(offset, offset+segLen).toCharArray();
				if(offset==0&&segLen==ss.text.length()&&ss.isNatrueSeg()){
					sequenceFreqContainer.addCharSequence(cs,Boolean.TRUE);
				}else{
					sequenceFreqContainer.addCharSequence(cs,Boolean.FALSE);
				}
			}
		}
		ss = null;
		
	}
	
	/**
	 * 封装串信息
	 * @author huangsui.cn
	 *
	 */
	class SequenceInfo{
		
		int type;
		int leftAdjacencyType;
		int rightAdjacenryType;
		String text;
		
		boolean isNatrueSeg(){
			if(this.leftAdjacencyType == OOV_ADJACENCY_NOSENSE
					&&this.rightAdjacenryType == OOV_ADJACENCY_NOSENSE){
				return true;
			}
			return false;
		}
		
		@Override
		public String toString(){
			return " ," + text + " " + leftAdjacencyType + " " + rightAdjacenryType;
		}
		
	}

}
